package com.example.authsystem.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.*;

import java.time.LocalDateTime;

@Entity
@Table(name = "users")
@Schema(
        name = "User",
        description = "用户实体类，存储系统中用户的基本信息和状态",
        example = """
                {
                  "id": 12345,
                  "username": "john_smith",
                  "email": "john.smith@example.com",
                  "status": "ACTIVE",
                  "lastLoginTime": "2024-01-15T10:30:00",
                  "createTime": "2024-01-10T09:00:00"
                }
                """
)
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Schema(
            description = "用户唯一标识符，系统自动生成",
            example = "12345",
            accessMode = Schema.AccessMode.READ_ONLY,
            minimum = "1"
    )
    @JsonProperty("id")
    private Long id;

    @Column(unique = true, nullable = false)
    @Schema(
            description = "用户名，系统内唯一标识符，用于登录和显示",
            example = "john_smith",
            requiredMode = Schema.RequiredMode.REQUIRED,
            minLength = 3,
            maxLength = 20,
            pattern = "^[a-zA-Z0-9_]+$"
    )
    @JsonProperty("username")
    private String username;

    @Column(unique = true, nullable = false)
    @Schema(
            description = "用户邮箱地址，用于登录凭据、找回密码和系统通知",
            example = "john.smith@example.com",
            requiredMode = Schema.RequiredMode.REQUIRED,
            format = "email",
            maxLength = 255
    )
    @JsonProperty("email")
    private String email;

    @Column(nullable = false)
    @Schema(
            description = "用户密码的加密哈希值，使用BCrypt算法加密存储",
            accessMode = Schema.AccessMode.WRITE_ONLY,
            maxLength = 60,
            format = "password"
    )
    @JsonIgnore
    private String passwordHash;

    @Column(nullable = false)
    @Schema(
            description = "用户账户状态，控制用户的系统访问权限",
            example = "ACTIVE",
            requiredMode = Schema.RequiredMode.REQUIRED,
            allowableValues = {"ACTIVE", "INACTIVE", "PENDING", "DISABLED", "LOCKED", "DELETED", "SUSPENDED"},
            defaultValue = "ACTIVE"
    )
    @JsonProperty("status")
    private String status;

    @Schema(
            description = "用户最后一次登录时间，用于安全审计和用户行为分析",
            example = "2024-01-15T10:30:00",
            accessMode = Schema.AccessMode.READ_ONLY
    )
    @JsonProperty("lastLoginTime")
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime lastLoginTime;

    @Column(nullable = false)
    @Schema(
            description = "用户账户创建时间，记录用户注册的准确时间",
            example = "2024-01-10T09:00:00",
            accessMode = Schema.AccessMode.READ_ONLY,
            requiredMode = Schema.RequiredMode.REQUIRED
    )
    @JsonProperty("createTime")
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime createTime;

    @Column(unique = true, nullable = false)
    @Schema(
            description = "用户手机号，用于登录和接收验证码",
            example = "13800138000",
            requiredMode = Schema.RequiredMode.REQUIRED,
            pattern = "^1[3-9]\\d{9}$", // 正则表达式：11 位，以1开头
            maxLength = 11
    )
    @JsonProperty("phone")
    private String phone;


    // Constructors
    public User() {
    }

    public User(String username, String email, String passwordHash, String phone) {
        this.username = username;
        this.email = email;
        this.passwordHash = passwordHash;
        this.phone = phone;
        this.status = "ACTIVE";
        this.createTime = LocalDateTime.now();
    }


    // Getters and Setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPasswordHash() {
        return passwordHash;
    }

    public void setPasswordHash(String passwordHash) {
        this.passwordHash = passwordHash;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public LocalDateTime getLastLoginTime() {
        return lastLoginTime;
    }

    public void setLastLoginTime(LocalDateTime lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

}
